﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Reflection;
using System.Web.UI;

/// <summary>
///BaseFormShow 的摘要说明
/// </summary>
public abstract class BaseFormShow:IAutoForm
{

    #region IAutoForm 成员

    public IEnumerable<System.Web.UI.Control> GetControls(PropertyInfo propertyInfo,object value)
    {
        Table tab = new Table();
        Literal lit = new Literal();
        if (propertyInfo.GetCustomAttributes(typeof(AutoFormDisplayNameAttribute), false).Length>0)
        {
            lit.Text = ((AutoFormDisplayNameAttribute)propertyInfo.GetCustomAttributes(typeof(AutoFormDisplayNameAttribute), false).First()).Name;
        }
        else
        {
            lit.Text = propertyInfo.Name;
        }
        TableCell cell1 = new TableCell();
        cell1.Controls.Add(lit);
        TableRow row = new TableRow();
        TableCell cell2 = new TableCell();
        tab.Rows.Add(row);
        row.Cells.Add(cell1);
        row.Cells.Add(cell2);

        object thisV = null;
        try
        {
            thisV = propertyInfo.GetValue(value, null);
        }
        catch (Exception ex)
        {
            //throw new Exception(propertyInfo.Name + "," + propertyInfo.PropertyType+","+propertyInfo.PropertyType.IsPrimitive);
        }

        foreach (Control c in GetFieldContrls(propertyInfo,thisV))
        {
            cell2.Controls.Add(c);
        }

        yield return tab;
    }

    #endregion

    public abstract IEnumerable<Control> GetFieldContrls(PropertyInfo propertyInfo,object value);



    #region IAutoForm 成员


    public abstract object GetValue(PropertyInfo propertyInfo, string value);
    

    #endregion
}
